package com.kexun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kexun.entity.DO.Department;
import com.kexun.entity.DO.DpShop;
import com.kexun.entity.DO.Shop;
import com.kexun.mapper.DepartmentMapper;
import com.kexun.mapper.DpShopMapper;
import com.kexun.service.DepartmentService;
import com.kexun.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private DpShopMapper dpShopMapper;

    @Autowired
    private ShopService shopService;

    @Override
    public PageInfo<Department> findDeptList(String dpName, int page, int limit) {
        PageHelper.startPage(page, limit);
        List<Department> deptList = departmentMapper.findDeptList(dpName);
        PageInfo<Department> departmentPageInfo = new PageInfo<>(deptList);
        return departmentPageInfo;
    }

    @Override
    public List<Department> findDeptById(int depId) {
        ArrayList<Department> departments = new ArrayList<>();
        List<Department> recursion = recursion(depId);
        departments.add(findByid(depId));
        departments.addAll(recursion);
        return getChildList(departments);
    }

    @Override
    public List<Department> findAllDepList() {
        return departmentMapper.selectList(null);
    }

    @Override
    @Transactional
    public void addDepartment(Department department) {
        //添加部门
        departmentMapper.insert(department);
        Integer dpId = department.getDpId();
        Integer[] shopList = department.getShopList();
        for (Integer shopId : shopList) {
            DpShop dpShop = new DpShop();
            dpShop.setDpId(dpId);
            dpShop.setShopId(shopId);
            dpShop.setCreateTime(new Date());
            dpShopMapper.insert(dpShop);
        }
    }

    @Override
    @Transactional
    public void deleteById(int id) {
        List<Shop> shopList = shopService.findShopByDeptId(id);
        for (Shop shop : shopList) {
            delDSbyDS(shop.getId(), id);
        }
        departmentMapper.deleteById(id);
    }

    @Override
    public Department findDepartmentById(int id) {
        return departmentMapper.selectById(id);
    }

    @Override
    @Transactional
    public void editDepartment(Department department) {
        Integer dpId = department.getDpId();
        Integer[] shopList = department.getShopList();
        HashSet<Integer> newIdSet = new HashSet<>();
        HashSet<Integer> oldIdSet = new HashSet<>();
        for (Integer shopId : shopList) {
            newIdSet.add(shopId);
        }
        List<Shop> shopByDeptId = shopService.findShopByDeptId(dpId);
        for (Shop shop : shopByDeptId) {
            oldIdSet.add(shop.getId());
        }
        HashSet<Integer> addSet = new HashSet<>();
        HashSet<Integer> delSet = new HashSet<>();
        for (int oldId : oldIdSet) {
            //如果新的的里面不存在之前有的就要删除
            if (!newIdSet.contains(oldId)) {
                delSet.add(oldId);
            }
        }
        for (int newId : newIdSet) {
            //如果老的的里面存在新的就要添加
            if (!oldIdSet.contains(newId)) {
                addSet.add(newId);
            }
        }
        for (Integer shopId : delSet) {
            delDSbyDS(shopId, dpId);
        }
        for (Integer shopId : addSet) {
            addDS(shopId, dpId);
        }
        departmentMapper.updateById(department);
    }

    @Override
    public void delDSbyDS(int shopId, int depId) {
        UpdateWrapper<DpShop> w = new UpdateWrapper<>();
        w.eq("dp_id", depId);
        w.eq("shop_id", shopId);
        dpShopMapper.delete(w);
    }

    private void addDS(int shopId, int depId) {
        DpShop dpShop = new DpShop();
        dpShop.setShopId(shopId);
        dpShop.setDpId(depId);
        dpShop.setCreateTime(new Date());
        dpShopMapper.insert(dpShop);
    }


    public List<Department> getChildList(List<Department> list) {
        ArrayList<Department> allList = new ArrayList<>();
        for (Department department : list) {
            allList.add(department);
            if (department.getChildList() != null && department.getChildList().size() > 0) {
                List<Department> childList = getChildList(department.getChildList());
                for (Department department1 : childList) {
                    allList.add(department1);
                }
            }

        }
        return allList;
    }


    private List<Department> recursion(int depId) {
        List<Department> list = findByPid(depId);
        for (Department department : list) {
            if (department.getDpParentId() == depId) {
                department.setChildList(recursion(department.getDpId()));
            }
        }
        return list;
    }

    public List<Department> findByPid(int pid) {
        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        wrapper.eq("dp_parent_id", pid);
        return departmentMapper.selectList(wrapper);
    }

    public Department findByid(int id) {
        return departmentMapper.selectById(id);
    }


}
